package com.ajun.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * Payment Service 熔断降级
 *
 * @author LiJun
 * @date 2021-07-13
 */
@Service
@Slf4j
public class PaymentService {

    /**
     * 正常访问
     */
    public String infoOk(Integer id) {
        return "线程池:" + Thread.currentThread().getName() + ",infoOk,id=" + id + "\t😀😀😀";
    }

    /**
     * 错误访问
     */
    @HystrixCommand(
            fallbackMethod = "infoTimeOutFallback",
            commandProperties = {
                    @HystrixProperty(
                            name = "execution.isolation.thread.timeoutInMilliseconds",
                            value = "3000"
                    )
            }
    )
    public String infoTimeOut(Integer id) {
        int timeNum = 3;
        // 模拟超时
        try {
            TimeUnit.SECONDS.sleep(timeNum);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "线程池:" + Thread.currentThread().getName() + ",infoOk,id=" + id + "\t😭😭😭" + timeNum + "秒";
    }

    public String infoTimeOutFallback(Integer id) {
        return "调用接口异常，降级，线程池:" + Thread.currentThread().getName() + ",infoTimeOutFallback,id=" + id + "\t😭😭😭";
    }
}
